package edu.turtlekit2.warbot;

import java.awt.Color;
import java.awt.Graphics;

import edu.turtlekit2.kernel.agents.Turtle;
import edu.turtlekit2.kernel.agents.Viewer;
import edu.turtlekit2.kernel.environment.Patch;

@SuppressWarnings("serial")
public class WarViewer extends Viewer{
	
	public void paintTurtle(Graphics g, Turtle t,int x,int y,int cellS){
		SingletonAffichage.getInstance(t.mySelf()).refreshScreen(g, t.mySelf(), cellS);
		g.setColor(t.color);
		if (((WarAgentsAbstract)t).getType().equals("WarExplorer")) {
			g.fillRect(x,y,cellS,cellS);
		}else if (((WarAgentsAbstract)t).getType().equals("WarBase")) {
			g.fillRect(x,y,10,10);
		}else if (((WarAgentsAbstract)t).getType().equals("WarRocketLauncher")) {
			g.fillRect(x,y,cellS,cellS);
		}else{
			g.fillRect(x,y,cellS,cellS);
		}
		g.drawString(((WarAgentsAbstract)t).getType()+" "+((WarAgentsAbstract)t).mySelf()+" : "+((WarDynamicAgentsAbstract)t).getTeam()+" - "+((WarDynamicAgentsAbstract)t).getEnergy()+" radius : "+((WarDynamicAgentsAbstract)t).getRadius(), x+10, y);
		g.drawOval(x-(((WarDynamicAgentsAbstract)t).getRadius()*2), y-(((WarDynamicAgentsAbstract)t).getRadius()*2), ((WarDynamicAgentsAbstract)t).getRadius() * 4, ((WarDynamicAgentsAbstract)t).getRadius() * 4);
		//g.drawRect(x-(((WarDynamicAgentsAbstract)t).getRadius()*2), y-(((WarDynamicAgentsAbstract)t).getRadius()*2), ((WarDynamicAgentsAbstract)t).getRadius() * 4, ((WarDynamicAgentsAbstract)t).getRadius() * 4);
	}
	
	public void paintPatch(Graphics g, Patch p,int x,int y,int cellS){
//		if(p.getTurtles().length == 0){
			g.setColor(p.color);
			g.fillRect(x,y,cellS,cellS);	
//		}
	}
	
	public void paintInfo(Graphics g){

		if(redrawAll){
			redrawAll = false;
			for (int i=envWidth-1; i >=0 ; i--)
				for (int j=envHeight-1; j >=0; j--)
					paintPatch(g, patchGrid[i][j],(i*cellSize) + xDecay,((envHeight-j-1)*cellSize)+yDecay,cellSize);
		}else
			for (int i=envWidth-1; i >=0 ; i--)
				for (int j=envHeight-1; j >=0; j--)
					if (patchGrid[i][j].change ){
						paintPatch(g, patchGrid[i][j],(i*cellSize) + xDecay,((envHeight-j-1)*cellSize)+yDecay,cellSize);
					}

		Turtle[] turtles = allTurtles.getTurtles();		
		for(int i=turtles.length-1;i>=0;i--)
		{
			if (turtles[i] != null && ! turtles[i].hidden)
				paintTurtle(g,turtles[i],(turtles[i].xcor()*cellSize)+xDecay,((envHeight-turtles[i].ycor()-1)*cellSize)+yDecay,cellSize);
		}
	}
}